﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WisDom.SmartTMS.Dto;
using WisDom.SmartTMS.Entity;
using WisDom.SmartTMS.InterFace;

namespace WisDom.SmartTMS.Services
{ 
    //IUserAppService实现类
    public class UserAppService : ApplicationService, IUserAppService
    {
        //依赖注入于
        private readonly IRepository<Users> _userRepository;

        public UserAppService(IRepository<Users> userRepository)
        {
            _userRepository = userRepository;
        }

        /// <summary>
        /// 添加账号
        /// </summary>
        /// <param name="userDtoCreate"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<UsersDtoCreate> Add([FromForm] UsersDtoCreate userDtoCreate)
        {
            Users user = new(
               GuidGenerator.Create(),
               userDtoCreate.UserNo,
               userDtoCreate.UserName,
               userDtoCreate.RoleId,
               userDtoCreate.PassWord
               );
            var result = await _userRepository.InsertAsync(user);
            return ObjectMapper.Map<Users, UsersDtoCreate>(result);
        }

        //异步登录
        [RemoteServiceAttribute(false)]
        public async Task<UsersDto> Get(string userNo, string password)
        {
            //async await做了个异步 可以使异步像同步执行 C#里面的语法糖
            var user= await _userRepository.GetAsync(m=>m.UserNo==userNo&&m.PassWord==password);
            //转换成用户Dto  原数据 映射数据
            var dto= ObjectMapper.Map<Users, UsersDto>(user);
            return dto;
        }
    }
}
